/*
栅栏
* */
import {Cell} from "./cell";

/**
 * 栅栏
 */
class Fence {
    cells = []
    specs
    title
    id

    constructor(specs) {
        this.specs = specs
        this.title = specs[0].key
        this.id = specs[0].key_id
    }

    init() {
        this._initCells()
    }

    _initCells() {
        this.specs.forEach(s => {
            // 去重
            // 循环判断数组中每一项 有的话返回 true 没有则 false
            const existes = this.cells.some(c => {
                return c.id === s.value_id
            })
            if (existes) {
                return
            }
            const cell = new Cell(s)
            this.cells.push(cell)
        })
    }

    setFenceSketch(skuList) {
        console.log('传递了', skuList)
        this.cells.forEach(c => {
            this._setCellSkuImg(c, skuList)
        })
        console.log('结束后', this.cells)
    }

    _setCellSkuImg(cell, skuList) {
        // 判断当前 cell 是否包含
        const specCode = cell.getCellCode()
        const matchedSku = skuList.find(s => s.code.includes(specCode))
        if (matchedSku) {
            cell.skuImg = matchedSku.img
        }
    }

    // pushValueTitles(title) {
    //   this.valueTitles.push(title)
    // }
}

export {
    Fence
}